﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EStart.Infrastructure.ClientData;
using Scaffold.API.Authentication;
using EStart.Infrastructure.Engines;
using EStart.Infrastructure.Caching;
using Scaffold.API.Filters;

namespace Scaffold.API.Controllers
{
    [APISecurityFilter]
    public class APIController : Controller
    {
        /// <summary>
        /// 根据用户获取token
        /// </summary>
        /// <param name="staffId"></param>
        /// <returns></returns>
        protected Token GetToken(Guid staffId)
        {
            var cacheManager = EngineContainerFactory.Context.GetInstance<ICacheManager>();
            //缓存有取缓存，缓存没有生成存入缓存
            var token = cacheManager.GetOrCreate(staffId.ToString(),
                entry => new Token
                {
                    StaffId = staffId,
                    SignToken = Guid.NewGuid(),
                    ExpireTime = DateTime.Now.AddDays(1)//一天过期
                });
            //返回token信息
            return token;
        }
        /// <summary>
        /// 清除Token
        /// </summary>
        /// <param name="staffId"></param>
        protected void ClearToken(Guid staffId)
        {
            var cacheManager = EngineContainerFactory.Context.GetInstance<ICacheManager>();
            cacheManager.Remove(staffId.ToString());
        }
    }
}
